package org.lanqiao.api.user;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.lanqiao.common.Constants;
import org.lanqiao.common.JsonResult;
import org.lanqiao.common.JsonResultWriter;
import org.lanqiao.pojo.UserPOJO;
import org.lanqiao.service.UserServiceImpl;
import org.lanqiao.util.StringUtil;

/**
 * Servlet implementation class SelectUserServlet
 */
@WebServlet(description = "这是一个查询用户信息的接口", urlPatterns = { "/api/user/select" })
public class SelectUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	UserServiceImpl usi = new UserServiceImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SelectUserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	    String userId=request.getParameter("userId");
	    //创建一个JsonResult封装消息，然后转为json，变成一个对象{}
        JsonResult result=null;
        //获得当前请求的Session对象，如果上下文中没有绑定的Session，创建一个session，如果有就返回session
        HttpSession session=request.getSession();
        List <HashMap<String,Object>>list=null;
        
        try {
            list = usi.select(userId);
            if(list!=null&&list.size()>0) {
                result=new JsonResult ("查询成功",Constants.STATUS_SUCCESS, list);
                
            }else {
                result=new JsonResult<LinkedHashMap<String,Object>>("该用户名不存在",Constants.STATUS_UNFOUND);
            }
        }catch(Exception ex){
            ex.printStackTrace();
            result=new JsonResult ("查询异常",Constants.STATUS_FAILURE,ex.getMessage());
        }
        
        //将响应和结果  数据，转为json
        JsonResultWriter.writer(response, result);        
        
	}

}
